<?php
class Links extends BaseClass {
	private static $_instance;

	private $selected_drills;

	//===================================================================================

	public function __construct($id = false) {
		parent::__construct();

		$this->selected_drills = array();
	}

	//===================================================================================

	public static function getInstance() {
		if (self::$_instance)
			return self::$_instance;
		return self::$_instance = new self();
	}

	//===================================================================================

	public function analize_incomming_request() {

		global $_SERVER, $G;

		$info = array();

		$info["requsted_uri"] = urldecode($_SERVER["REQUEST_URI"]);

		/// Dividing query string into separate parts
		$info["parts"] = array();
		$info["selected"] = array();

		$parts = explode("/", substr($info["requsted_uri"], 1));

		foreach ($parts as $part) {
			$part_id = strpos($part, "-");
			if ($part_id === false)
				continue;
			$part_value = substr($part, $part_id + 1);
			$part_id = substr($part, 0, $part_id);

			$info["parts"][$part_id] = $part_value;
		}


		/// Fetching signle params

		if (isset($info["parts"]["vehicle"])) {
			$info["selected"]["vehicle"] = $info["parts"]["vehicle"];
		}

		if (isset($info["parts"]["year"])) {
			$info["selected"]["year"] = $info["parts"]["year"];
		}

		if (isset($info["parts"]["price"])) {
			$info["selected"]["price"] = $info["parts"]["price"];
		}

		if (isset($info["parts"]["currency"])) {
			$info["selected"]["currency"] = $info["parts"]["currency"];
		}

		if (isset($info["parts"]["ec"])) {
			$info["selected"]["exterior_color"] = $info["parts"]["ec"];
		}

		if (isset($info["parts"]["et"])) {
			$info["selected"]["engine_type"] = $info["parts"]["et"];
		}

		if (isset($info["parts"]["ev"])) {
			$info["selected"]["engine_volume"] = $info["parts"]["ev"];
		}

		if (isset($info["parts"]["tt"])) {
			$info["selected"]["transmission_type"] = $info["parts"]["tt"];
		}

		if (isset($info["parts"]["ts"])) {
			$info["selected"]["transmission_steps"] = $info["parts"]["ts"];
		}


		/// Fetching info about make, model and modification
		if (isset($info["parts"]["used"])) {
			$selector = explode(".", $info["parts"]["used"]);

			if (isset($selector[0]) && !empty($selector[0])) {
				$info["selected"]["make"] = $selector[0];
			}

			if (isset($selector[1]) && !empty($selector[1])) {
				$info["selected"]["model"] = $selector[1];
			}

			if (isset($selector[2]) && !empty($selector[2])) {
				$info["selected"]["modification"] =  $selector[2];
			}
		}


		/// Fetching info about location
		if (isset($info["parts"]["loc"])) {
			$selector = explode(".", $info["parts"]["loc"]);

			if (isset($selector[0]) && !empty($selector[0])) {
				$info["selected"]["state"] = $selector[0];
			}

			if (isset($selector[1]) && !empty($selector[1])) {
				$info["selected"]["city"] = $selector[1];
			}
		}

		$this->selected_drills = $info["selected"];
		/// It's more easy to build future links then values are just empty
		foreach ($G["ad_drills"] as $drill_name => $drill_value) {
			if (!isset($this->selected_drills[$drill_name]))
				$this->selected_drills[$drill_name] = "";
		}


		return $info;
	}

	//===================================================================================

	private function appendLinkPart($url, $new_part) {

		if (empty($new_part))
			return $url;

		if ($url[strlen($url) - 1] == "/") {
			return $url.$new_part;
		}

		return $url."/".$new_part;
	}

	//===================================================================================

	public function compileLink($action, $drill) {

		global $G;

		if ($action == "add") {
			/// Merge already selected by user drills, and current_dril (which is a part of future link)
			$selected = array_merge($this->selected_drills, $drill);
		} elseif ($action == "remove") {
			/// Diff already selected by user drills, and current_dril (which is a part of future link)
			$selected = $this->selected_drills;
			$selected[key($drill)] = "";
		}

		$url = "/";


		if ($selected["vehicle"]) {
			$url = $this->appendLinkPart($url, "vehicle-".$selected["vehicle"]);
		}


		$m3 = "";
		if ($selected["make"] || $selected["model"] || $selected["modification"]) {
			if ($selected["make"]) {
				$m3 .= $selected["make"];
			}
			if ($selected["model"]) {
				$m3 .= ".".$selected["model"];
			}
			if ($selected["modification"]) {
				if (!$selected["model"]) $m3 .= ".";
				$m3 .= ".".$selected["modification"];
			}

			if (!empty($m3))
				$m3 = "used-".$m3;
		}
		$url = $this->appendLinkPart($url, $m3);


		if ($selected["year"]) {
			$url = $this->appendLinkPart($url, "year-".$selected["year"]);
		}


		$loc = "";
		if ($selected["state"] || $selected["city"]) {
			if ($selected["state"]) {
				$loc .= $selected["state"];
			}
			if ($selected["city"]) {
				$loc .= ".".$selected["city"];
			}

			if (!empty($loc))
				$loc = "loc-".$loc;
		}
		$url = $this->appendLinkPart($url, $loc);


		if ($selected["price"] || $selected["price"] === "0") {
			$url = $this->appendLinkPart($url, "price-".$selected["price"]);
		}


		if ($selected["currency"]) {
			$url = $this->appendLinkPart($url, "currency-".$selected["currency"]);
		}

		if ($selected["exterior_color"]) {
			$url = $this->appendLinkPart($url, "ec-".$selected["exterior_color"]);
		}

		if ($selected["engine_type"]) {
			$url = $this->appendLinkPart($url, "et-".$selected["engine_type"]);
		}

		if ($selected["engine_volume"] || $selected["engine_volume"] === "0") {
			$url = $this->appendLinkPart($url, "ev-".$selected["engine_volume"]);
		}

		if ($selected["transmission_type"]) {
			$url = $this->appendLinkPart($url, "tt-".$selected["transmission_type"]);
		}

		if ($selected["transmission_steps"]) {
			$url = $this->appendLinkPart($url, "ts-".$selected["transmission_steps"]);
		}

		return $url;
	}
}

?>
